function [cc, cl] = clustercoords(vol, meth, kthr)
% clustercoords  - get list of clusters in volume
%
% FORMAT:       [cc, cl] = clustercoords(vol [, meth [, kthr]])
%
% Input fields:
%
%       vol         XxYxZ logical array, true for indices to be clustered
%       meth        either of 'face', 'edge', or 'vertex' (default: face)
%       kthr        cluster-size threshold (default: 1)
%
% Output fields:
%
%       cc          cluster coords, Cx4 list of coordinates, where
%                   the 4th column is a cluster index within 1...N
%       cl          1xN cell array with Px3 coordinates
%
% Note: this now uses the compiled function clustercoordsc !!!

% Version:  v0.7f
% Build:    8110521
% Date:     Nov-05 2008, 9:00 PM CET
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 1 || ...
   ~islogical(vol) || ...
    isempty(vol)
    error( ...
        'BVQXtools:BadArgument', ...
        'Bad or missing argument.' ...
    );
end
if nargin < 2 || ...
   ~ischar(meth) || ...
    isempty(meth)
    meth = 'f';
else
    meth = lower(meth(1));
end
if ~any('efv' == meth)
    meth = 'f';
end
if nargin < 3 || ...
   ~isa(kthr, 'double') || ...
    numel(kthr) ~= 1 || ...
    isinf(kthr) || ...
    isnan(kthr) || ...
    kthr < 1 || ...
    kthr > numel(vol)
    kthr = 1;
else
    kthr = round(kthr);
end
switch (meth)
    case {'e'}
        meth = 2;
    case {'f'}
        meth = 1;
    otherwise
        meth = 3;
end

% check for compiled function
try
    [cn1, cn2, cc, cl] = clustercoordsc(vol, meth, kthr);
catch
    rethrow(lasterror);
end
